import modelAbstract from "./modelAbstract";
import {images} from "../service/image";
import config from '../config'
import bullet from "../canvas/bullet"
import { directionEnum } from '../directionEnum';
import util from '../util'
import wall from "../canvas/wall";
import steel from "../canvas/steel";
import boss from "../canvas/boss";
import tank from "../canvas/tank";
import player from "../canvas/player";
export default class extends modelAbstract implements IModel{
    canvas: ICanvas = bullet;
    name: string = "bullet";
    constructor(public tank:IModel){
        super(tank.x + config.model.width/2, tank.y + config.model.height/2);
        this.direction = tank.direction as unknown as directionEnum;
    }
    image(): HTMLImageElement {
        return images.get(this.name as keyof typeof config.images)!
    }
    render(): void {
        let x = this.x;
        let y = this.y;
        let step = this.tank.name == 'player' ? 10 : 1;
        switch (this.direction){
            case directionEnum.top:
                y -= step;
                break;
            case directionEnum.right:
                x += step;
                break;
            case directionEnum.bottom:
                y += step;
                break;
            case directionEnum.left:
                x -= step;
                break;
        }
        const touchModel = util.isModelTouch(x,y,2,2,[...wall.models,...steel.models,...boss.models,...tank.models,...player.models]);
        if(util.isCanvasTouch(x,y,2,2)){
            this.destroy();
        }else if(touchModel && touchModel.name != this.tank.name){
            this.destroy();
            if(touchModel.name != 'steel') touchModel.destroy();
            this.blast(touchModel);
        }else{
            this.x = x;
            this.y = y;
            this.draw();
        }
    }

    protected draw(){
        this.canvas.ctx.drawImage(this.image(),this.x,this.y,2,2);
    }
}